package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import domain.User;
import service.Service;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet(name = "getCondition", value = "/getCondition")
public class getCondition extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String src = req.getParameter("src");
//        System.out.println(src);
//        System.out.println(src);
//        System.out.println("2");
//        String username = req.getParameter("username");
//        Service service = new Service();
//        resp.getWriter().write(service.getCondition(src,username));

//        Object user = req.getSession().getAttribute("user");
//        //序列化成json往前传
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.writeValue(resp.getOutputStream(),user);
//        System.out.println("调用getuser");
//        从session取之前存入的user
        String src = req.getParameter("src");
        User user = (User) req.getSession().getAttribute("user");

        System.out.println(user.getUsername());

        Service service = new Service();
        String colCondition=service.getCondition(src,user.getUsername());
        String likeCondition=service.getLikeCondition(src,user.getUsername());

//        System.out.println(colCondition+"xxxxxx"+likeCondition);
//        System.out.println(String.valueOf(condition)+"这里是");
//        resp.getWriter().write(String.valueOf(condition));

        List<String> list = Arrays.asList(colCondition, likeCondition);

        ObjectMapper mapper = new ObjectMapper();
        resp.setContentType("application/json;charset=utf-8");
        mapper.writeValue(resp.getWriter(),list);
        System.out.println("getCondi");

    }
}
